package com.example.manager.controller;

import com.example.manager.entity.PageBean;
import com.example.manager.entity.User;
import com.example.manager.entity.UserPageBean;
import com.example.manager.service.Imple.UserService;
import com.example.manager.utils.JsonResult;
import com.google.gson.Gson;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;
    Logger logger = LoggerFactory.getLogger(this.getClass().getName());
//      登录
    @RequestMapping(value = "login",method = RequestMethod.POST)
    public String login(User user, HttpSession session,Model model){
        logger.info(user.toString());
        JsonResult<User> result = userService.login(user);
        if (result.getState() == 140){
            model.addAttribute("msg",result.getMessage());
            return "login";
        } else if (result.getState() == 150) {
            model.addAttribute("msg",result.getMessage());
            return "login";
        }
        if (result.getData().getAdmin()==1){
            model.addAttribute("admin","admin");

        }
        logger.info(String.valueOf(result.getData().getAdmin()));
        session.setAttribute("user",result.getData());

        return "/index";
    }


//      注册功能
    @RequestMapping(value = "register",method = RequestMethod.POST)
    public String register(User user, @Param("repassword")String repassword, Model model, HttpSession session){
        logger.info(user.toString()+""+repassword);

//        调用userService的register方法传user和repassword（验证两次密码是否相同）
        Integer result = userService.register(user,repassword);
        session.setAttribute("flag","");
        if (result == 110) {
            model.addAttribute("msg","用户名长度要大于2");
            return "register";
        } else if (result == 120) {
            model.addAttribute("msg","用户名已存在");
            return "register";
        } else if (result == 130) {
            model.addAttribute("msg","两次密码不相同，请重新输入");
            return "register";
        }
        return "redirect:/login";
    }




//      前往用户管理页面
    @RequestMapping("showuser")
    public String showuser(String page,Model model){
        System.out.println(page);
        int currentpage = 0;
        if (page == null){
            currentpage = 1;
        }else {
            currentpage = Integer.parseInt(page);
        }

        UserPageBean pageBean = new UserPageBean();
        pageBean = userService.getUserByPage(currentpage,5);
        model.addAttribute("pageBean",pageBean);
        logger.info(pageBean.toString());

        return "showuser";
    }
//      删除功能
    @RequestMapping("/delUser")
    public String delUser(String username){
//        调用userService的delUser方法通过用户名删除用户
        userService.delUser(username);
        return "redirect:/user/showuser";
    }

//      注销功能
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        //使session失效
        session.invalidate();

        return "redirect:/login";

    }
//      搜索用户
    @RequestMapping("searchuser")
    public String search(String page,Model model,@Param("search") String search){
        System.out.println("search:"+search);
        int currentpage = 0;
        if (page == null){
            currentpage = 1;
        }else {
            currentpage = Integer.parseInt(page);
        }

        UserPageBean pageBean = new UserPageBean();
        pageBean = userService.findByUsername(search,5);
        model.addAttribute("pageBean",pageBean);
        logger.info(pageBean.toString());

        return "showuser";
    }

//    用户详情
    @RequestMapping("useredit")
    public String touseredit(String username,Model model){
        User user = new User();
        user = userService.findUserByUsername(username);
        model.addAttribute("user",user);
        return "useredit";
    }

//    用户个人信息
    @RequestMapping("userinfo")
    public String userinfo(String username,Model model){
        User user = new User();
        user = userService.findUserByUsername(username);
        model.addAttribute("user",user);
        return "userinfo";
    }

    @RequestMapping("updateuserinfo")
    public String updateuserinfo(User user, Model model, RedirectAttributes redirectAttributes){

        userService.pudateUserInfo(user);
        redirectAttributes.addAttribute("username",user.username);
        return "redirect:/user/userinfo";
    }
//      修改密码
    @RequestMapping("updatepw")
    public String updatepw(String password,String newpassword,String repassword,String username,Model model){
        Integer result = userService.updatepw(password,newpassword,repassword,username);
        if (result==210){
            model.addAttribute("msg","两次密码不相同");
            return "updatepw";
        } else if (result == 220) {
            model.addAttribute("msg","旧密码不相同");
            return "updatepw";
        }

        return "login";
    }
}
